'use strict'
let openid = ''//通过查询数据库获得

class sendtimeletter{

  constructor (mdb) {
    this.mdb = mdb || null
  }
  async get() {
   // setInterval(()=>{
        //send发送订阅消息,当时光信的时间与当前时间对应的话
    //获取当前时间
        let date = new Date()
        let year = date.getFullYear()
        let month = date.getMonth()+1;
        let day = date.getDate();
        let hour = date.getHours();
        let minute = date.getMinutes();
        let now=year+'-'+month+'-'+day+' '+hour+':'+minute;
        console.log(now);

        // 2、发送模板消息
        let sql=`select * from timeletter where time=${now}`
        let r=await this.mdb.query(sql)
        let result=r['rows']
        //如果存在相同的时间
        if(result){
            result.map((item,idx)=>{
                let requestData = {
                    "touser": item.openid,
                    "template_id": "tyT9WPxkv1J38yXfCLxs9nHt4HyVXaKHV7CJfwqMBZo",
                    "page": "index",
                    "lang": "zh_CN",
                    "data": {
                    "thing1": {
                        "value": "时光信"
                        },
                        "time3": {
                            "value": now
                        },
                        "thing2": {
                            "value": "提醒你看时光信"
                        }
                    }
                }
                let num=result.length;
                let access_token=await c.service.model.wxlogin.get()
                console.log('检测access_token的值：',access_token)
                let res = await request
                .post(`https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=${access_token}`)
                .send(requestData)
                .set('Accept', 'application/json')
                console.log(res)
                return res
            })
            
        }
    //},60000)



    
}
 
}

module.exports = sendtimeletter
